package com.rxt.component;

import com.rxt.config.RabbitMQConfig;
import com.rxt.model.domain.OOrders;
import com.rxt.service.IOOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 消息消费者
 */
@Slf4j
@Component
public class DeadLetterQueueConsumer {

    @Autowired
    private IOOrdersService ordersService;

    // 监听延迟队列A
    @RabbitListener(queues = RabbitMQConfig.DELAY_QUEUE_NAME)
    public void receiveA(Message message) {
        String orderId = new String(message.getBody());
        log.info("当前时间: {},  延迟队列收到订单编号: {}", LocalDateTime.now(), orderId);

        // 根据订单编号查询订单
        OOrders order = ordersService.getById(Long.parseLong(orderId));

        if (order != null && order.getStatus()==1
                && order.getDeleteStatus()==0 ) {
            // 设置订单的状态为已取消
            order.setStatus(6);
//            LambdaQueryWrapper<OOrders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(OOrders::getStatus,1);
            // 修改状态
            boolean result = ordersService.updateById(order);
        }
    }
}

